<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2019/9/2
 * Time: 17:13
 */

namespace app\plat\controller;


use think\Db;

class SiteUser extends Base
{
    public $table = 'site_user';
    public $site_id;

    public function __construct()
    {
        parent::__construct();
        $this->site_id = input('site_id');
    }


    public function index()
    {
        $keyword = input('keyword');
        $this->getSite();
        if($keyword){
            $where['name'] = ['like','%'.$keyword.'%'];
        }
        if(!isset($where))
            $where = '1=1';

        $data['list'] = Db::name($this->table)
            ->alias('a')
            ->where('a.site_id',$this->site_id)
            ->where($where)
            ->order('a.id desc')

            ->paginate();
        $data['page'] = $data['list']->render();

        $this->assign('data',$data);
        $this->assign('keyword',$keyword);

        return $this->fetch();
    }

    public function add()
    {
        $this->getSite();

        if(request()->isPost()){
            return $this->save();
        }

        $this->assign('data',1);
        $this->assign('title','新增管理员');
        $this->assign('url',url('add'));
        return $this->fetch('info');
    }

    public function update()
    {
        $id = input('id/d');

        $data = Db::name($this->table)->where('id',$id)->find();
        if(!$data){
            $this->error('不存在');
        }
        $this->getSite($data['site_id']);

        if(request()->isPost()){
            return $this->save();
        }
        $data['password'] = '';

        $this->assign('data',json_encode($data));
        $this->assign('title','编辑管理员');
        $this->assign('url',url('update'));
        return $this->fetch('info');
    }

    public function save()
    {
        $id = input('id/d');
        $param = input();
        $data = [
            'remark'=>input('remark'),
            'enable'=>input('enable'),
            'modtime'=>time(),
        ];
        if(!$this->checkDataNode())
            return dataReturn(0,'数据权限错误');
        if($id){
            //编辑
            if($param['password'] != ''){
                $data['password'] = md5($param['password']);
            }
            $a = Db::name($this->table)->where('id',$id)->update($data);
        }else{
            //新增
            $data['username'] = input('username');
            if($data['username'] == '')
                return dataReturn(0,'用户名不能为空');
            if(unique_add_value($data['username'],$this->table,'username')){
                $this->error('该用户名已存在');
            }
            $data['password'] = md5($param['password']);
            $data['addtime'] = time();
            $super = Db::name($this->table)->where('site_id',$param['site_id'])->where('is_super',1)->find();
            if($super)
                return dataReturn(0,'只能创建一个超管账号');
            $data['is_super'] = $super?0:1;
            $data['log_time'] = 0;
            $data['log_ip'] = '';
            $data['site_id'] = input('site_id');
            $a = Db::name($this->table)->insert($data);
        }
        if($a){
            echo api_return(1,'保存成功');
        }else{
            echo api_return(0,'保存失败');
        }
        return;
    }

    public function getSite($site_id = '')
    {
        $site_id = $site_id == ''?input('site_id'):$site_id;
        $site = Db::name('site')->where('id',$site_id)->find();
        if(!$site)
            $this->error('站点不存在');

        $this->assign('site',$site);
        return $site;
    }

    /**
     * 新添是否用户名唯一
     */
    public function ajax_unique_username()
    {
        $username = input('username');
        if(unique_add_value($username,$this->table,'username')){
            echo api_return(1,'已存在');
        }else{
            echo api_return(0,'不存在');
        };
    }
}